﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PasteDataObject.cs" company="">
//   
// </copyright>
// <summary>
//   The paste data object.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.WindowsForms.Controls
{
    using System.Collections.Generic;
    using System.Windows.Forms;

    using Libium.Core;

    /// <summary>
    /// The paste data object.
    /// </summary>
    public class PasteDataObject : ICopyData
    {
        #region Fields

        /// <summary>
        /// The _data.
        /// </summary>
        private readonly IDataObject _data;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PasteDataObject"/> class.
        /// </summary>
        /// <param name="data">
        /// The data.
        /// </param>
        public PasteDataObject(IDataObject data)
        {
            _data = data;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the formats.
        /// </summary>
        public IEnumerable<string> Formats
        {
            get
            {
                return _data.GetFormats();
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The get value.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <param name="action">
        /// The action.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        /// <exception cref="NotExistsException">
        /// </exception>
        public T GetValue<T>(string format, NotExistsAction action = NotExistsAction.ThrowException)
        {
            if (_data.GetDataPresent(format, false))
            {
                return (T)_data.GetData(format, false);
            }

            if (action == NotExistsAction.ThrowException)
            {
                throw new NotExistsException("Data in format <" + format + "> doesn't exist");
            }

            return default(T);
        }

        /// <summary>
        /// The is exist.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool IsExist(string format)
        {
            return _data.GetDataPresent(format, false);
        }

        /// <summary>
        /// The set value.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        public void SetValue(string format, ICopyValue value)
        {
            _data.SetData(format, false, value.GetValue());
        }

        #endregion
    }
}